import app from '../../main'
import { login } from '@/api/user'
import * as types from '@/store/mutation-types'
const user = {
    namespaced: true,
    state: {
        //属性
        token: '',
        loginInfo: {},
        currentUser: {},
    },

    getters: {
        [types.ACCESS_TOKEN](state) {
            return state.token
        },
        [types.LOGIN_INFO](state) {
            return state.loginInfo
        },
    },
    mutations: {
        [types.ACCESS_TOKEN](state, value) {
            state.token = value
            if (value) {
                app.config.globalProperties.ls.set(types.ACCESS_TOKEN, value)
            } else {
                app.config.globalProperties.ls.remove(types.ACCESS_TOKEN)
            }
        },
        [types.LOGIN_INFO](state, value) {
            state.loginInfo = value
        },
    },
    actions: {
        // 登录
        Login({ commit }, userInfo) {
            return new Promise((resolve, reject) => {
                login(userInfo).then(response => {
                    const result = response.data.result
                        // 
                        // commit(types.ACCESS_TOKEN, result.token)
                        // commit(types.LOGIN_INFO, result)
                        // 调用其他命名空间
                        // commit(`fund/${types.USER_FUNDS}`, res[1].data.result.items, { root: true })

                    resolve(response)
                }).catch(error => {
                    reject(error)
                })
            })
        },

    }

}
export default user